<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @cancel="handleCancel"
    v-drag
  >
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="loadData(1)">
        <a-row :gutter="24">
          <a-col :xl="12" :lg="14" :md="16" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('业务类型')">
              <a-select v-model="queryParam.type">
                <a-select-option :value="item.value" :key="index" v-for="(item, index) in typeList" placeholder="请选择业务类型"
                >{{ item.text }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('操作员')">
              <a-select v-model="queryParam.createBy">
                <a-select-option v-for="(item, index) in userList" :value="item.value" :key="index" placeholder="请选择操作员">{{
                    item.text
                  }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('充值方式')">
              <a-select v-model="queryParam.rechargeType">
                <a-select-option :value="item.value" :key="index" v-for="(item, index) in rechargeTypeList" placeholder="请选择充值方式"
                >{{ item.text }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('支付方式')">
              <a-select v-model="queryParam.payType">
                <a-select-option :value="item.value" :key="index" v-for="(item, index) in payTypeList" placeholder="请选择支付方式"
                >{{ item.text }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="loadData(1)" icon="search">{{$t('查询')}}</a-button>
              <a-button
                type="primary"
                icon="download"
                style="margin-left: 8px"
                @click="handleExportXls($t('账户流水'))"
                >{{$t('导出')}}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        :columns="columns"
        :dataSource="dataSource"
        size="middle"
        bordered
        class="j-table-force-nowrap"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
      </a-table>
    </div>
    <template slot="footer">
      <a-row :gutter="20">
        <a-col :span="24">
          <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
        </a-col>
      </a-row>
    </template>
  </a-modal>
</template>

<script>
import { getAction, downFile } from '@api/manage'
import {initDictOptions} from "@comp/dict/JDictSelectUtil";
let _self
export default {
  name: 'AccountFlowModal',
  data() {
    return {
      // 表头
      columns: [
        {
          title:this.$t('业务时间'),
          align:"center",
          dataIndex: 'createTime'
        },
        {
          title:this.$t('业务类型'),
          align:"center",
          dataIndex: 'type_dictText'
        },
        {
          title: this.$t('充值方式'),
          align: 'center',
          dataIndex: 'rechargeType_dictText'
        },
        {
          title: this.$t('支付方式'),
          align: 'center',
          dataIndex: 'payType_dictText'
        },
        {
          title:this.$t('交易前余额'),
          align:"center",
          dataIndex: 'beginBalance'
        },
        {
          title:this.$t('交易余额'),
          align:"center",
          dataIndex: 'busiBalance'
        },
        {
          title:this.$t('交易后余额'),
          align:"center",
          dataIndex: 'endBalance'
        },
        {
          title:this.$t('操作员'),
          align:"center",
          dataIndex: 'createBy'
        }
      ],
      url: {
        list: '/gift/accountFlow',
        exportXlsUrl: "/gift/accountFlowExportXls",
      },
      queryParam: {
        accountId: this.accountId,
        column: 'createTime',
        order: 'desc'
      },
      dataSource: [],
      loading: false,
      toggleSearchStatus: true,
      ipagination: {
        current: 1,
        pageSize: 30,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + this.$t('sum-total',{total:total})
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        onShowSizeChange: (current, pageSize) => this.handleSizeChange(current, pageSize),
      },
      typeList: [],
      rechargeTypeList: [],
      payTypeList: [],
      userList: [],
      title:this.$t('账户流水'),
      visible: false
    }
  },
  props: {
    accountId: String,
    required: true
  },
  created() {
    this.initDictConfig()
    this.loadData(1)
  },
  mounted() {
    this.$nextTick(() => {})
  },
  methods: {
    handleExportXls(fileName) {
      if (!fileName || typeof fileName != 'string') {
        fileName = this.$t('导出文件')
      }
      downFile(this.url.exportXlsUrl, this.queryParam).then((data) => {
        if (!data) {
          this.$message.warning(this.$t('文件下载失败'))
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
    initDictConfig() {
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.type'}).then((res) => {
        if (res.success) {
          this.typeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.recharge_type'}).then((res) => {
        if (res.success) {
          this.rechargeTypeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'bis_yard_account_flow.pay_type'}).then((res) => {
        if (res.success) {
          this.payTypeList = res.result
        }
      })
      getAction('/gift/dict',{dictCode: 'sys_user,realname,realname'}).then((res) => {
        if (res.success) {
          this.userList = res.result
        }
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error(this.$t('请设置url.list属性'))
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.queryParam
      params.accountId = this.accountId
      if(params.createTime_begin === '' ||params.createTime_begin ===undefined){
        delete params.createTime_begin
      }
      if(params.createTime_end === '' ||params.createTime_end ===undefined){
        delete params.createTime_end
      }
      if(params.type === '' ||params.type ===undefined){
        delete params.type
      }
      if(params.createBy === '' ||params.createBy ===undefined){
        delete params.createBy
      }
      params.pageNo = this.ipagination.current;
      params.pageSize = this.ipagination.pageSize;
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    modalFormOk() {
      this.loadData(1)
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      console.log(pagination)
      this.ipagination = pagination;
      this.loadData();
    },    
    handleSizeChange(current, pageSize) {
      this.ipagination.pageSize = pageSize
    },
    
  },
}
</script>

<style scoped>
</style>